package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    @Operation(summary = "上传声音")
    @PostMapping("/uploadTrack")
    public Result uploadTrack(MultipartFile file) {
        Map<String, Object> map = (Map<String, Object>) trackInfoService.uploadTrack(file);
        return Result.ok(map);
    }

    @Operation(summary = "保存声音")
    @PostMapping("/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody @Validated TrackInfo trackInfo) {
        Long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
        trackInfoService.saveTrackInfo(userId);
        return Result.ok();
    }

    @Operation(summary = "根据用户Id，状态等条件查询声音列表")
    @PostMapping("/findUserTrackPage/{pageNo}/{pageSize}")
    public Result findUserTrackPage(@PathVariable Long pageNo, @PathVariable Long pageSize, @RequestBody TrackInfoQuery trackInfoQuery) {
        Long userId = AuthContextHolder.getUserId();
        long l = userId == null ? 1l : userId;
        Page<TrackInfoVo> trackInfoVoPage = new Page<>(pageNo, pageSize);

        IPage<TrackInfoVo> iPage = this.trackInfoService.findUserTrackPage(trackInfoVoPage);
        return Result.ok(iPage);
    }

	@Operation(summary = "根据声音Id 删除声音对象")
	@DeleteMapping("/removeTrackInfo/{trackId}")
	public Result removeTrackInfo(@PathVariable Long trackId) {
		return trackInfoService.removeTrackInfo(trackId);
	}

	@Operation(summary = "根据声音Id回显声音对象")
	@GetMapping("/getTrackInfo/{trackId}")
	public Result getTrackInfo(@PathVariable Long trackId) {
		TrackInfo trackInfo = this.trackInfoService.getById(trackId);
		return Result.ok(trackInfo);
	}

	@Operation(summary = "修改声音")
	@PutMapping("/updateTrackInfo/{trackId}")
	public Result updateTrackInfo(@PathVariable Long trackId, @RequestBody @Validated TrackInfoVo trackInfoVo) {
		trackInfoService.updateTrackInfo(trackId,trackInfoVo);
		return Result.ok();
	}
}

